/**
 * outsider.com.cn Inc.
 * Copyright (c) 2011-2022 All Rights Reserved.
 */
package com.yuanmeng.common.core.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yuanmeng.common.core.domain.R;
import com.yuanmeng.common.core.exception.ServiceException;

import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.tm.api.GlobalTransactionContext;

/**
 * @author outsider
 * @version Id: RemoteValidateHandler.java, v 0.1 2022/6/17 11:15 outsider Exp $$
 */
public class RemoteValidateHandler {
    private static final Logger log = LoggerFactory.getLogger(RemoteValidateHandler.class);

    /**
     * 熔断服务调用返回异常handler
     * @param response
     */
    public static void remoteServiceFailHandler(R response) {
        if (R.FAIL == response.getCode()) {
            //手动回滚
            try {
                GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            } catch (TransactionException e) {
                log.error("GlobalTransactionContext.rollback异常：{}", e.getMessage());
                e.printStackTrace();
            }
            throw new ServiceException(response.getMsg());
        }
    }
}